<template>
	<view class="order-detail">
		<template v-if="detail">
		<view v-if="jxsFlag === 'jxs'">
			<view v-if="address" class="order-address" @tap="jumpTo">
				<view class="icon-wrap"><i class="iconfont icondizhi1"></i></view>
				<view class="address-text">
					<view class="address">{{address.addressDet}}</view>
					<view class="info"><text class="name">{{address.pickupName}}</text>{{address.pickupMobile}}</view>
				</view>
				<view class="right-arrow"><i class="iconfont iconxiangyou"></i></view>
			</view>
			<view class="order-address" v-else>
				<view class="icon-wrap"><i class="iconfont icondizhi1"></i></view>
				<view class="address-text">
					<view class="address">请先去cms添加收货地址，再进行兑换</view>
				</view>
			</view>
		</view>
		<view class="order-section pd1">
		<!-- 	<view class="order-number" v-if="jxsFlag === 'jxs'">
				<view class="number-left">
					<text>{{detail.orderCode}}</text>
				</view>
			</view> -->
				<view class="tui-goods-item goods-item" v-for="item in list">
				<!-- 经销商 -->
				<template v-if="jxsFlag==='jxs'">
					<view class="tui-goods-img">
						<image :src="imgSrc + item.imgRelativeUri" />
					</view>
					<view class="tui-goods-info">
						<view class="tui-goods-info-inner">
							<view class="tui-goods-title">{{item.goodsName}}</view>
							<!-- <view class="tui-goods-sub">创建日期：<text>{{item.payTime}}</text></view> -->
							<view class="tui-price-box">
								<view class="tui-goods-price">{{item.goodsPoints}}<text class="small-size">积分</text></view>
								<view class="tui-goods-num">x{{item.goodsQty}}</view>
							</view>
						</view>
					</view>
				</template>
				<!-- 非经销商 -->
				<template v-else>
					<view class="tui-goods-img">
						<image :src="imgSrc + item.imgRelativeUri" />
					</view>
					<view class="tui-goods-info">
						<view class="tui-goods-info-inner">
							<view class="tui-goods-title">{{item.couponName}}</view>
							<view class="tui-price-box">
								<view class="tui-goods-price">{{item.goodsPoints}}<text class="small-size">积分</text></view>
								<view class="tui-goods-num">x{{item.couponQty}}</view>
							</view>
						</view>
					</view>
				</template>
			</view>
			
			<view class="points-part">
				<view class="user-item">
					<view class="left">支付方式</view>
					<!-- <view class="right">{{jxsFlag === 'jxs'?'积分':detail.payType}}</view> -->
					<view class="right">积分</view>
				</view>
				<view class="user-item">
					<view class="left">总计</view>
					<view class="right red">{{detail.orderTotalPoints}}积分</view>
					
				</view>
				<view class="user-item">
					<view class="left">支付积分</view>
					<view class="right red">{{detail.orderTotalPoints}}积分</view>
				</view>
			</view>
		</view>
		
		<view class="order-foot-btns">
			<view class="foot-text">
				合计：{{detail.orderTotalPoints}} <text class="small-size">积分</text>
			</view>
			<tui-button class="btn" type="danger" width="200rpx" height="70rpx" :size="28" shape="circle" @tap="submitOrder">提交订单</tui-button>
		</view>
		</template>
		<!-- 经销商提示地址，非经销商提示确认 -->
		<tui-modal :show="modalShow" @click="handleClick" @cancel="hide" title="提示" :content="modalContent"></tui-modal>
		
		<loading></loading>
		
		<tui-modal></tui-modal>
	</view>
</template>

<script>
	import tuiButton from '@/components/button/button';
	import resultPage from '@/components/resultPage/resultPage.vue';
	export default {
		components: {
			tuiButton,
			resultPage,
		},
		data() {
			return {
				imgSrc: this.$store.state.imgUrl,
				jxsFlag: '',
				detail:null,
				list:[],
				address:null,
				modalShow: false,
				modalContent: '',
				goodsInfo:null,//购物车结算的数据
				couponId:'',//非经销商优惠券ID
				couponQty:'',//优惠券数量
				allPoints: 0, //总积分
				dealerId:'',//经销商id
				from:'',
				pageName:'',//跳转来的页面
			}
		},
		onLoad(options) {
			if(options&&options.pageName){
				this.pageName=options.pageName
			}
			this.from=options.from;
			// #ifdef H5
			   this.$wechat.noShare();
			// #endif
			if (this.from=='agent') {//经销商
				this.jxsFlag = 'jxs';
				if(!this.address){
					if(options&&options.goodsInfo){
						this.goodsInfo=JSON.parse(options.goodsInfo)
					}
					this.dealerId=uni.getStorageSync('agencyId');
					this.getDealerAddress({
						num:1,
						size:10,
					})
					this.dealerOrderConfirm();//获取商品详情
				}
				
			} else {
				this.jxsFlag= 'notjxs'
				if(options&&options.couponId){
					this.couponId=options.couponId;
					this.couponQty=options.couponQty;
				}
				this.notDealerOrderConfirm();
			}
			 
		},
		onShow(){
			if (this.from=='agent') {//经销商
				this.jxsFlag = 'jxs';
				if (uni.getStorageSync('pointAddress')) {
					this.address=JSON.parse(uni.getStorageSync('pointAddress'))
					uni.removeStorageSync('pointAddress');
				}
			}
		},
		methods: {
			dealerOrderConfirm() {//经销商
				let params={
					goodsInfo:this.goodsInfo
				}
				this.$http.dealerOrderConfirm(params).then((res) => {
					if (res.status === 0) {
						this.detail=res.data;
						this.list=res.data.orderGoodsDet;
						this.list.forEach(item=>{
							this.allPoints=+item.totalPoints
						})
					}
				})
			},
			notDealerOrderConfirm(){//非经销商，参数有待确认
				let params={
					couponId:this.couponId,
					couponQty:this.couponQty,
				}
				
				this.$http.notDealerOrderConfirm(params).then((res) => {
					if (res.status === 0) {
						this.detail=res.data;
						this.list=[res.data.records];
						
						
					}
				})
			},
			getDealerAddress(page) {//经销商地址
			
				let params = {
					current: page.num,
					pageSize: page.size,
					dealerId:this.dealerId,
				}
				this.$http.getDealerAddress(params).then((res) => {
					if (res.status === 0) {
						this.address=res.data.records[0];
					}
				})
			},
			jumpTo(){//跳转到收货地址
				//dealerId
				uni.navigateTo({
					url:`/packageD/pages/deliveryAddress/deliveryAddress?dealerId=${this.dealerId}`
					
				})
			},
			
			submitOrder(){
				if (this.jxsFlag === 'jxs'){//经销商
					if(!this.address){
						uni.showToast({
							icon: 'none',
							title: '地址为空无法提交',
							duration: 1000,
						})
						return;
					}else{
						this.modalShow=true;
						this.modalContent="支付后不得更改退单，请您核对订单信息，无误后再进行操作"
					}
				}else{
					this.modalShow=true;
					this.modalContent="支付后不得更改退单，请您核对订单信息，无误后再进行操作"
				}
				
			},
			handleClick(e) {
				let index = e.index;
				if(index === 1){
					if (this.jxsFlag === 'jxs'){//经销商
						let params={
							orderGoodsDet:this.list,
							addressId:this.address.id,
						}
						this.$http.submitDealerOrder(params).then((res) => {
							if (res.status === 0) {
								uni.redirectTo({
									url: `/packageD/pages/pointsOrder/submitOrderResult?from=${this.from}&pageName=${this.pageName}&id=${res.data.id}`
								});
								//购物车
								this.$eventHub.$emit('refreshPointsCart')
							}
						})
					}else{
						let params={
							couponId:this.couponId,
							couponQty:this.couponQty,
						}
						this.$http.submitNotDealerOrder(params).then((res) => {
							if (res.status === 0) {
								uni.redirectTo({
									url: `/packageD/pages/pointsOrder/submitOrderResult?from=${this.from}&id=${res.data.id}`
								})
							}
						})
					}
				}
				this.hide();
			},
			hide() {
				this.modalShow = false;
			},
		}
	}
</script>

<style lang="scss">
	@import "@/packageD/static/style/common.scss";
</style>
